n = int(input())
stones = list(map(int, input().split()))
dp = [{stones[i]} for i in range(n)]  # 使用空集占位
stones = [0] + stones
dp = [0] + dp
max_length = 1
for i in range(n, 0, -1):
    if i + stones[i] <= n and len(dp[i + stones[i]]) < len(dp[i].union(dp[i + stones[i]])):
        dp[i + stones[i]] = dp[i].union(dp[i + stones[i]])
    if 2 * i <= n and len(dp[2 * i]) < len(dp[i].union(dp[2 * i])):
        dp[2 * i] = dp[i].union(dp[2 * i])
    max_length = max(max_length, len(dp[i]))
print(max_length)
